package com.backend.demo1.global;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;

public class GlobalLog {
    private static final Logger logger = LoggerFactory.getLogger(GlobalLog.class);

    // 带格式化的 info 方法
    public static void info(String format, Object... arguments) {
        logger.info(format, arguments);
    }

    public static void debug(String format, Object... arguments) {
        logger.debug(format, arguments);
    }

    public static void warn(String format, Object... arguments) {
        logger.warn(format, arguments);
    }

    public static void error(String format, Object... arguments) {
        logger.error(format, arguments);
    }

    public static void error(String message, Throwable throwable) {
        logger.error(message, throwable);
    }

    public static void trace(String format, Object... arguments) {
        logger.trace(format, arguments);
    }

  // 通用日志记录方法，接受日志级别参数
    public static void logByHttpStatus(int code, String message, Throwable throwable, Level level) {
        switch (level) {
            case ERROR -> logger.error("HTTP {} - {}: {}", code, message, throwable.getMessage(), throwable);
            case WARN -> logger.warn("HTTP {} - {}: {}", code, message, throwable.getMessage());
            case INFO -> logger.info("HTTP {} - {}", code, message);
            case DEBUG -> logger.debug("HTTP {} - {}", code, message);
            default -> logger.trace("HTTP {} - {}", code, message);
        }
    }

    public static void logByHttpStatus(int code, String message, Throwable throwable) {
        logByHttpStatus(code, message, throwable, determineLogLevelByCode(code));
    }

    // 静态方法：根据状态码确定日志级别
    public static Level determineLogLevelByCode(int code) {
        if (code >= 500) {
            return Level.ERROR;
        } else if (code >= 400) {
            return Level.WARN;
        } else {
            return Level.INFO;
        }
    }
}